//
//  HisDetailViewController.swift
//  ShouChong
//
//  Created by yw on 2024/9/18.
//

import UIKit
import MJRefresh

class HisDetailViewController: BaseViewController {

    @IBOutlet weak var tableView: UITableView!
    var hisVM:HistoryViewModel = HistoryViewModel()
    var list:[HistoryModel] = []
    var page:Int = 1
    var size:Int = 10
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        loadData()
        
    }

    private func loadData() {
        let sendData = [
            "page":String(self.page),
            "size":"10"
        ]
        hisVM.getHisByPage(sendData: sendData) { models in
            DPrint(models)
            if models.count == 0 {
                self.tableView.mj_footer?.endRefreshingWithNoMoreData()
            } else {
                self.tableView.mj_footer?.endRefreshing()
                self.list += models
                self.tableView.reloadData()
            }
            
        }
    }
    
    private func setupUI() {
        self.title = "详情"
        
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.backgroundColor = UIColor(named: "color_bg")
        self.tableView.register(UINib(nibName: "HisDetailCell", bundle: nil), forCellReuseIdentifier: "HisDetailCellID")
//        let mjHeader = MJRefreshNormalHeader.init(refreshingBlock: { [weak self] in
//            
//            self?.loadData()
//        })
//        mjHeader.lastUpdatedTimeLabel?.isHidden = true
//        mjHeader.stateLabel?.isHidden = true
//        self.tableView.mj_header = mjHeader
//        self.tableView.mj_header?.beginRefreshing()
        
        let mjFooter = MJRefreshAutoNormalFooter.init { [weak self] in
            self?.page += 1
            self?.loadData()
        }
//        mjFooter.stateLabel?.isHidden = true
        self.tableView.mj_footer = mjFooter
//        self.tableView.mj_footer?.isHidden = true
    }

    deinit {
        DPrint("\(#function)")
    }
}

extension HisDetailViewController:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        DPrint(hisVM.list.count)
        return self.list.count
    }
    
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = HisDetailCell.createHisDetailView(tableView: tableView , index: indexPath)
        cell.model = self.list[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 178
    }
    
}
